package com.gpzx.zx.module.mine.frame.view;

import android.app.Dialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;


import com.gpzx.zx.module.mine.ui.activity.LogoutAccountActivity;
import com.just.agentweb.AgentWeb;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wkq.base.frame.mosby.delegate.MvpView;
import com.wkq.base.utils.StatusBarUtil2;
import com.wu.common.R;

import com.wu.common.module.ui.WebViewActivity;
import com.wu.common.utils.AlertDialogUtils;
import com.wu.common.utils.AlertUtil;
import com.wu.common.utils.DoublePressed;
import com.wu.common.utils.StatusBarUtil;

/**
 * 作者: 吴奎庆
 * <p>
 * 时间: 2019/3/22
 * <p>
 * 简介:
 */
public class LogoutAccountView implements MvpView, View.OnClickListener {
   public  LogoutAccountActivity mContext;
    //注销说明的url
    String url = null;
    //协议的url
    String zxxzUrl = null;
    private Dialog dialog;

    public LogoutAccountView(LogoutAccountActivity mContext) {
        this.mContext = mContext;
    }

    public void initView() {
        if (mContext == null) return;
        mContext.binding.setClick(this);
        StatusBarUtil.setStatusBarDarkMode(mContext);
        StatusBarUtil2.setColor(mContext, mContext.getResources().getColor(R.color.color_2b2b2b), 0);
        QMUITopBar toolbar = (QMUITopBar) mContext.binding.includeToolbar;
        toolbar.setTitle("审核申请");
        toolbar.setBackgroundColor(mContext.getResources().getColor(R.color.color_2b2b2b));
        toolbar.addLeftImageButton(R.drawable.ic_arrow_back_white_24dp, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.finish();
            }
        });

        mContext.binding.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mContext.binding.btLogoutAccount.setBackgroundResource(R.drawable.shape_logout_account_true_bg);
                } else {
                    mContext.binding.btLogoutAccount.setBackgroundResource(R.drawable.shape_logout_account_false_bg);

                }
            }
        });
        showWeb();

    }

    /**
     * 展示  注销的说明
     */
    private void showWeb() {
        if (mContext == null) return;
        if (TextUtils.isEmpty(url)) url = "https://wukuiqing49.github.io/app_comment/xy/yhxy.html";

        mContext.mAgentWeb = AgentWeb.with(mContext)
                .setAgentWebParent(mContext.binding.fl, new FrameLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(url);

    }

    @Override
    public void onClick(View v) {
        if (DoublePressed.onDoublePressed()) return;
        int id = v.getId();
        if (id == R.id.bt_logout_account) {
            if (mContext.binding.checkbox.isChecked()) {
                processLogout();
            } else {
                showMessage("请阅读,注销协议");
            }
        } else if (id == R.id.tv_protocol) {
            showProtocal();
        }
    }

    /**
     * 处理注销逻辑
     */
    private void processLogout() {
        if (mContext == null) return;
        dialog = AlertDialogUtils.showTwoButtonDialog(mContext,
                "否",
                "是",
                "注销后将清除您的数据,您确定要注销此账户吗?",
                R.color.color_999,
                R.color.color_23d4, new AlertDialogUtils.DialogTwoListener() {
                    @Override
                    public void onClickLeft(Dialog dialog) {
                        if (dialog != null) dialog.dismiss();
                    }

                    @Override
                    public void onClickRight( Dialog dialog) {
                        if (dialog != null) dialog.dismiss();

                        showAggainDialog();
                    }
                });

    }

    private void showAggainDialog() {
        if (mContext == null) return;
        dialog = AlertDialogUtils.showTwoButtonDialog(mContext,
                "取消",
                "我知道了",
                "注销后将清除您的数据,您确定注销吗?",
                R.color.color_999,
                R.color.color_23d4, new AlertDialogUtils.DialogTwoListener() {
                    @Override
                    public void onClickLeft(Dialog dialog) {
                        if (dialog != null) dialog.dismiss();
                    }

                    @Override
                    public void onClickRight(Dialog dialog) {
                        if (dialog != null) dialog.dismiss();
                        mContext.getPresenter().logoutAccount(mContext);
                    }
                });

    }

    /**
     * 跳转协议页面
     */
    private void showProtocal() {
        if (TextUtils.isEmpty(zxxzUrl)) zxxzUrl = "https://htmlpreview.github.io/?https://github.com/wukuiqing49/app_comment/blob/master/xy/zxzh.html";
        WebViewActivity.Companion.newInstance(mContext,"https://htmlpreview.github.io/?https://github.com/wukuiqing49/app_comment/blob/master/xy/zxzh.html","注销协议");
    }

    public void showMessage(String s) {
        if (mContext == null || TextUtils.isEmpty(s)) return;
        AlertUtil.showDeftToast(mContext, s);
    }


}
